#include "registerwin.h"
#include "ui_registerwin.h"
#include <QPushButton>
#include <QDebug>
#include <QFileDialog>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
registerWin::registerWin(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::registerWin)
{
    ui->setupUi(this);
    sqlmodel = new QSqlTableModel(this);
    sqlmodel->setTable("employee");
    connect(ui->bto_clear,&QPushButton::clicked,this,&registerWin::on_clear_click);

}

registerWin::~registerWin()
{
    delete ui;
}

void registerWin::timerEvent(QTimerEvent *e)
{
    cv::Mat matImg;
    cap.read(matImg);
    cv::cvtColor(matImg,matImg,cv::COLOR_BGR2RGB);
    QImage img(matImg.data,matImg.cols,matImg.rows,matImg.step1(),QImage::Format_RGB888);
    QPixmap qmap = QPixmap::fromImage(img);

   // qmap.scaled(ui->showhead->size());
    ui->showhead->setPixmap(qmap);

}

void registerWin::on_clear_click()
{
    ui->name->clear();
    ui->jobnum->clear();
    ui->sex->clear();
    ui->number->clear();
    ui->day->setDate(QDate::currentDate());
}

void registerWin::on_openCap_clicked()
{
    if(ui->openCap->text()=="打开摄像头")
    {
        ui->openCap->setText("关闭摄像头");
        cap.open(0);
        if(!cap.isOpened())
           {
            qDebug()<<"open cap fail"<<endl;
        }
        timeid = startTimer(100);

    }else{
        ui->openCap->setText("打开摄像头");
        cap.release();
        killTimer(timeid);
        ui->showhead->clear();
    }


}

void registerWin::on_selectFile_clicked()
{
    QString filePath = QFileDialog::getOpenFileName(this);
    if(filePath.isEmpty())
    {
        qDebug()<<"没选择";
        return;
    }
    ui->filePath->setText(filePath);
    QImage img(filePath);

    ui->showhead->setPixmap(QPixmap::fromImage(img).scaled(ui->showhead->size()));

}

void registerWin::on_capter_clicked()
{
    ui->openCap->setText("打开摄像头");
    cap.read(capterSave);
    cap.release();
    killTimer(timeid);

    cv::cvtColor(capterSave,capterSave,cv::COLOR_BGR2RGB);
    QImage img(capterSave.data,capterSave.cols,capterSave.rows,capterSave.step1(),QImage::Format_RGB888);
    QPixmap qmap =QPixmap::fromImage(img);
    ui->showhead->setPixmap(qmap);

}

void registerWin::on_bto_register_clicked()
{
    int64_t faceid;
    //create table if not exists employee(ID integer primary key autoincrement,name varchar(256),
    //sex varchar(32),job text,birthday text,phone text,faceID integer unique,headfile text)
    facemanage facem;
    faceid = facem.register_face(capterSave);
    if(faceid<0)
    {
        qDebug()<<"register fail"<<endl;

    }
    qDebug()<<faceid<<endl;
    QImage img(capterSave.data,capterSave.cols,capterSave.rows,capterSave.step1(),QImage::Format_RGB888);
    QString saveFile=QString("C:/Users/13275/Desktop/embedded_Project/FaceDetection/faceProjectServer/faceDetectServer/%1.jpg").arg(ui->name->text());
    img.save(saveFile,"jpg",100);
    int row = sqlmodel->rowCount();
    sqlmodel->insertRow(sqlmodel->rowCount());
    sqlmodel->setData(sqlmodel->index(row,1),ui->name->text());
    sqlmodel->setData(sqlmodel->index(row,2),ui->sex->text());
    sqlmodel->setData(sqlmodel->index(row,3),ui->jobnum->text());
    sqlmodel->setData(sqlmodel->index(row,4),ui->day->text());
    sqlmodel->setData(sqlmodel->index(row,5),ui->number->text());
    sqlmodel->setData(sqlmodel->index(row,6),faceid);
    sqlmodel->setData(sqlmodel->index(row,7),saveFile);
    sqlmodel->submitAll();
}
